﻿@using Pear.Web.Core
@inherits PearRazorPage<object>
@{ Layout = "~/Views/Shared/_Layout.cshtml"; }
@{ ViewData["Title"] = "部门新增";}
@section header
{

}
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">

                <div class="layui-form-item">
                    <label class="layui-form-label">上级部门</label>
                    <div class="layui-input-block">
                        <ul id="ParentDepartment" class="dtree" data-id="null"></ul>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" lay-verify="name" autocomplete="off" placeholder="请输入名称" class="layui-input" />
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">图标</label>
                    <div class="layui-input-block">
                        <input type="text"  name="icon" id="icon" lay-verify="icon" autocomplete="off" placeholder="请输入图标" class="layui-input" />
                    </div>
                </div>


                <div class="layui-form-item">
                    <label class="layui-form-label">电话号码</label>
                    <div class="layui-input-block">
                        <input type="text" name="telephone" autocomplete="off" placeholder="请输入电话号码" class="layui-input" />
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">传真号码</label>
                    <div class="layui-input-block">
                        <input type="text" name="fax" lay-verify="authorize" autocomplete="off" placeholder="请输入传真号码" class="layui-input" />
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">邮箱</label>
                    <div class="layui-input-block">
                        <input type="text" name="email" lay-verify="email" autocomplete="off" placeholder="请输入邮箱" class="layui-input" />
                    </div>
                </div>



                <div class="layui-form-item">
                    <label class="layui-form-label">显示排序</label>
                    <div class="layui-input-block">
                        <input type="text" name="sequence" lay-verify="sequence" autocomplete="off" placeholder="请输入排序号码" class="layui-input" value="1" />
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <input type="checkbox" name="enabled" value="true" lay-skin="switch" lay-text="启用|禁用" lay-filter="enabled" checked />
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block">
                        <textarea type="text" name="remark" lay-verify="remark" autocomplete="off" placeholder="请输入备注" class="layui-input layui-textarea"></textarea>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit="" lay-filter="department-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
@section footer
{
    <script>
        layui.use(['form', 'jquery', 'http', 'popup', 'dtree', 'iconPicker'], function () {
            let form = layui.form;
            let $ = layui.jquery;
            let http = layui.http;
            let popup = layui.popup;
            let dtree = layui.dtree;
            let iconPicker = layui.iconPicker;

            


            dtree.renderSelect({
                elem: "#ParentDepartment",
                initLevel: "3",
                method: 'get',
                url: "/api/department/all",
                dataStyle: "layuiStyle",
                dataFormat: "list",
                accordion: true,
                line:true,
                response: {
                    statusName: "statusCode", //返回标识（必填）
                    statusCode: 200, //返回码（必填）
                    title:"name",
                }
            });

            iconPicker.render({
                elem: '#icon',
                type: 'fontClass',
                page: false
            });

            form.on('submit(department-save)', function (data) {

                data.field.parentId = data.field.ParentDepartment_select_nodeId || null;

                if (data.field.enabled == "true") {
                    data.field.enabled = true;
                } else {
                    data.field.enabled = false;
                }

                http.post('/api/department/add', JSON.stringify(data.field)).then((result) => {
                    if (result.succeeded) {
                        popup.success("保存成功", function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.refresh();
                        });
                    } else {
                        popup.failure("保存失败");
                    }
                });
                return false;
            });
        })
    </script>
}